<template>
	<form @submit="formSubmit">
		<view class="login">
			<image mode="aspectFill" class="login-bg" src="../../static/img/loginbg.png" />
			<view class="login-inner">
				<image class="login-logo" src="../../static/img/logo.png">
					<view class="input-row">
						<input name="userPhone" placeholder="用户名" :value="userPhone" />
					</view>
					<view class="input-row">
						<input name="userPwd" placeholder="密码" :value="userPwd" password="true" />
					</view>
					<button class="cu-btn primary block" form-type="submit" :disabled="subdisabled">登录</button>
					<view class="login-footer">
						<view @click="onForgetPwd" class="login-link">忘记密码</view>
						<view class="login-footer__divider"></view>
						<view @click="onRegister" class="login-link">注册新用户</view>
					</view>
			</view>
		</view>
	</form>
</template>
<script>
	const graceChecker = require("../../common/graceChecker.js")
	export default {
		data() {
			return {
				subdisabled: false,
				userPhone: '', //15986696448  13924669271
				userPwd: '' //123456	654321
			}
		},
		onLoad() {
			let loginUser = uni.getStorageSync("loginUser");
			if(loginUser && loginUser.username
			   && loginUser.pwd){
				console.log(loginUser.username, loginUser.pwd);
				this.userPhone = loginUser.username;
				this.userPwd = loginUser.pwd;
			}
		},
		methods: {
			formSubmit: function(e) {
				uni.showLoading({
					mask: true,
					title: "登录中..",
				})
				this.subdisabled = true;
				let formData = e.detail.value;
				let rule = [{
						name: "userPhone",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "手机不能为空"
					},
					{
						name: "userPwd",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "密码不能为空"
					},

				];

				let checkRes = graceChecker.check(formData, rule);
				if (checkRes) {
					this.signIn(formData.userPhone, formData.userPwd);
					this.userPhone = formData.userPhone;
					this.userPwd = formData.userPwd;
				} else {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
					this.subdisabled = false;
				}
			},
			async signIn(usercell, pwd) {
				let result = await this.loginService.signinAsync(usercell, pwd);

				if (result && result.RetBool) {
					this.UserInfo.RetId = result.RetId;
					this.UserInfo.RetObject = result.RetObject;
					this.UserInfo.mobile = usercell;
					let retObject = JSON.parse(result.RetObject);
					let limitsAuto = retObject.Auth;
					if (limitsAuto) {
						this.UserInfo.Jurisdiction = limitsAuto.split(",");
					}

					if (this.UserInfo != null && this.UserInfo.RetId != '') {
						//if(!this.$ENV.prodEnv){
						uni.switchTab({
							url: "../index/index",
							success: () => {
								this.subdisabled = false;
								console.log(this.UserInfo);
								uni.setStorageSync("loginUser", {
									username: usercell,
									pwd: pwd
								})
							}
						})
						// }else{
						// 	uni.redirectTo({
						// 		url: "../indextabbar/indextabbar",
						// 		success: () => {
						// 			this.subdisabled = false;
						// 			console.log(this.UserInfo);
						// 		}
						// 	})
						// }
					}
				} else {
					this.subdisabled = false;
					uni.hideLoading();
					uni.showToast({
						title: result.RetMsg,
						icon: "none"
					})
				}


			},
			onForgetPwd() {
				uni.navigateTo({
					url: '/pages/forgetpwd/index'
				})
			},
			onRegister() {
				uni.navigateTo({
					url: '/pages/register/index'
				})
			},


		}
	}
</script>
<style lang="scss">
	.login {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;

		&-bg {
			left: 0;
			top: 0;
			position: absolute;
			z-index: 1;
			width: 100%;
			height: 100%;
		}

		&-inner {
			position: absolute;
			padding: 0 80rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-top: 238rpx;
			box-sizing: border-box;
			z-index: 2;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
		}

		&-logo {
			width: 426rpx;
			height: 112rpx;
			margin-bottom: 100rpx;
		}

		.cu-btn {
			margin-top: 70rpx;
			height: 80rpx;
			width: 100%;
			background-color: #3d85db;
			box-shadow: 0px 0px 36rpx 13rpx rgba(0, 0, 0, 0.15);
			border-radius: 40rpx;
		}

		.input-row {
			display: flex;
			flex-direction: row;
			align-items: center;
			width: 100%;
			margin-top: 40rpx;

			input {
				height: 80rpx;
				border: 1px solid white;
				font-size: 30rpx;
				flex: 1;
				color: #ffffff;
				border-radius: 40rpx;
				text-align: center;
			}
		}

		&-footer {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			margin-top: 45rpx;
			position: absolute;
			bottom: 50rpx;

			&__divider {
				width: 1px;
				height: 32rpx;
				background-color: #fbfffe;
			}
		}

		&-link {
			color: white;
			font-size: 30rpx;
			padding: 0 50rpx;
		}
	}
</style>
